package cn.tedu._03mvcboot01.controller;

import cn.tedu._03mvcboot01.pojo.dto.UserLoginDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/*
@Controller 标记当前类为一个控制器类，SpringMvc会自动扫描控制器类，并生成实例，注入容器中
作用接收请求，处理请求，返回响应结果
 */
@Controller
public class UserController {
    // 需要处理登录请求的接口,返回登录成功
    // 请求地址：localhost:8080/v1/users/login
    @RequestMapping("/v1/users/login")
    @ResponseBody
    public String login(){
        //不允许直接返回一个字符串
        return  "登录成功";
    }

    //服务器端接收参数
    //1.HttpServletRequest 接收
    @RequestMapping("/v1/users/login1")
    @ResponseBody
    public String login1(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return "ServletRequest：用户名"+username+"密码"+password;
    }

    //2.通过参数类型接收
    @RequestMapping("/v1/users/login2")
    @ResponseBody
    public String login2(String username,String password){
        return "参数类型：用户名"+username+"密码"+password;
    }

    //3.通过DTO类方式接收
    @RequestMapping("/v1/users/login3")
    @ResponseBody
    public String login3(UserLoginDTO userLoginDTO){
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        return "DTO:用户名"+username+"密码"+password;
    }

}
